package ModelLayer;

import java.util.ArrayList;

/**
 * Write a description of class DVDContainer here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class DVDContainer
{
    private static DVDContainer instance;
    private ArrayList<DVD> dvds;
    private DVD dvd;

    /**
     * Constructor for objects of class DVDContainer
     */
    public DVDContainer()
    {
        dvds = new ArrayList<DVD>();
    }

    public static DVDContainer getInstance()
    {
        if(instance == null)
        {
            instance = new DVDContainer();
        }
        return instance;
    }

    public boolean addDVD(DVD dvd)
    {
        boolean added = false;
        if(findDVDByName(dvd.getTitle()) == null && findDVD(dvd.getBarcode()) == null)
        {
            dvds.add(dvd);
            added = true;
        }
        return added;
    }

    public DVD findDVDByName(String title)
    {
        for(int i=0; i<dvds.size(); i++)
        {
            if(dvds.get(i).getTitle().equals(title))
            {
                return dvds.get(i);
            }
        }
        return null;
    }

    public DVD findDVD(int barcode)
    {
        for(int i=0; i<dvds.size(); i++)
        {
            if(dvds.get(i).getBarcode() == barcode)
            {
                return dvds.get(i);
            }
        }
        return null;
    }

    public void deleteDVD(int barcode)
    {
        if(dvds.contains(findDVD(barcode)))
        {
            dvds.remove(findDVD(barcode));
        }
    }

    public void deleteDVDByName(String title)
    {
        if(dvds.contains(findDVDByName(title)))
        {
            dvds.remove(findDVDByName(title));
        }
    }

    //     public boolean addDVDCopy(String title, String serialNumber, String purchaseDate, String purchasePrice)
    //     {
    //         return findDVDByName(title).addNewCopy(serialNumber,purchaseDate,purchasePrice);
    //     }

    public void addDVDCopy(String title, String serialNumber, String purchaseDate, String purchasePrice)
    {
        //findDVDByName(title).addNewCopy(serialNumber,purchaseDate,purchasePrice);
        boolean exist = false;
        for (int i=0; i<findDVDByName(title).getCopyList().size();i++)
        {
            if(findDVDByName(title).getCopyList().get(i).getSerialNumber().equals(serialNumber))
            {
                exist=true;
            }
        }

        if(!exist)
        {
            findDVDByName(title).addNewCopy(serialNumber,purchaseDate,purchasePrice);
            System.out.println("A new copy has been added!");
        }
        else
        {
            System.out.println("A copy of this DVD with the same serial number already exists!");
        }
    }

    public Copy getDVDCopy(String title)
    {
        return findDVDByName(title).getCopy();
    }
}
